package cn.bdqn.service.impl;

import cn.bdqn.dto.Dto;
import cn.bdqn.mapper.ItripHotelRoomMapper;
import cn.bdqn.mapper.ItripHotelTempStoreMapper;
import cn.bdqn.mapper.ItripLabelDicMapper;
import cn.bdqn.mapper.ItripProductStoreMapper;
import cn.bdqn.pojo.ItripHotelRoom;
import cn.bdqn.pojo.ItripHotelTempStore;
import cn.bdqn.pojo.ItripLabelDic;
import cn.bdqn.service.ItripHotelRoomService;
import cn.bdqn.vo.ItripHotelRoomVO;
import cn.bdqn.vo.ItripHotelRoomReceive;
import cn.bdqn.vo.ItripLabelDicVO;
import cn.bdqn.vo.StoreVO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ItripHotelRoomServiceImpl implements ItripHotelRoomService {

    @Resource
    private ItripHotelTempStoreMapper itripHotelTempStoreMapper;
    @Resource
    private ItripHotelRoomMapper itripHotelRoomMapper;

    @Resource
    private ItripProductStoreMapper itripProductStoreMapper;

    @Resource
    private ItripLabelDicMapper itripLabelDicMapper;

    //查询酒店房间
    @Override
    public Dto selectHotelRoom(ItripHotelRoomReceive itripHotelRoomReceive) {
        Dto dto = new Dto();
        List<ItripHotelRoom> list = new ArrayList<ItripHotelRoom>(0);
        List<List<ItripHotelRoomVO>> result = new ArrayList<List<ItripHotelRoomVO>>(0);
//        Map<String,Object> map=new HashMap<String, Object>();
//        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        if (itripHotelRoomReceive.getHotelId() == null | itripHotelRoomReceive.getHotelId().equals("")) {
            dto.setMsg("酒店id不能为空");
            return dto;
        }

        if (itripHotelRoomReceive.getStartDate() == null | itripHotelRoomReceive.getEndDate() == null) {
            dto.setMsg("入住或退房时间不能为空");
            return dto;
        }
        if (itripHotelRoomReceive.getStartDate().getTime() >= itripHotelRoomReceive.getEndDate().getTime()) {
            dto.setMsg("入住时间不能早于退房时间");
            return dto;
        }
//        map.put("hotelId",itripHotelRoomReceive.getHotelId());
//        map.put("startDate",sdf.format(itripHotelRoomReceive.getStartDate()));
//        map.put("endDate",sdf.format(itripHotelRoomReceive.getEndDate()));
//
//        map.put("isBook",itripHotelRoomReceive.getIsBook());
//        map.put("isCancel",itripHotelRoomReceive.getIsCancel());
//        map.put("isHavingBreakfast",itripHotelRoomReceive.getIsHavingBreakfast());
//        map.put("isTimelyResponse",itripHotelRoomReceive.getIsTimelyResponse());
//        map.put("payType",itripHotelRoomReceive.getPayType());
//        map.put("roomBedTypeId",itripHotelRoomReceive.getRoomBedTypeId());
//        System.out.println(map.size());
        list = itripHotelRoomMapper.selectByCondition(itripHotelRoomReceive);
        if (list.size() > 0 && !list.isEmpty()) {
            for (int i = 0; i < list.size(); i++) {
                List<ItripHotelRoomVO> roomList=new ArrayList<>(0);
                ItripHotelRoomVO ihrb = new ItripHotelRoomVO();
                Integer store = null;
                ihrb.setId(list.get(i).getId());
                ihrb.setHotelId(list.get(i).getHotelId());
                ihrb.setRoomTitle(list.get(i).getRoomTitle());
                ihrb.setIsBook(list.get(i).getIsBook());
                ihrb.setIsCancel(list.get(i).getIsCancel());
                ihrb.setIsHavingBreakfast(list.get(i).getIsHavingBreakfast());
                ihrb.setIsTimelyResponse(list.get(i).getIsTimelyResponse());
                ihrb.setPayType(list.get(i).getPayType());
                ihrb.setRoomBedTypeId(list.get(i).getRoomBedTypeId());
                ihrb.setRoomPrice(list.get(i).getRoomPrice());
                List<ItripHotelTempStore> hTSLists = list.get(i).getHTSList();
                for (int j = 0; j < hTSLists.size(); j++) {
                    if (hTSLists.get(j).getStore() != null && store == null) {
                        store = hTSLists.get(j).getStore();
                    }
                    if (store != null && hTSLists.get(j).getStore() != null && store > hTSLists.get(j).getStore()) {
                        store = hTSLists.get(j).getStore();
                    }
                }
                if (store != null) {
                    ihrb.setStore(store);
                } else {
                    ihrb.setStore(itripProductStoreMapper.selectByPId(ihrb.getId()).getStore());
                }
                roomList.add(ihrb);
                result.add(roomList);
            }
            dto.setCode("0");
            dto.setMsg("查询成功");
            dto.setSuccess("true");
            dto.setData(result);
            return dto;
        } else {
            dto.setCode("10086");
            dto.setMsg("查询失败");
            dto.setSuccess("false");
            return dto;
        }
    }

    @Override
    public Dto selectByTypes(Long parentId) {
        Dto dto = new Dto();
        List<ItripLabelDic> labelDics = new ArrayList<ItripLabelDic>(0);
        List<ItripLabelDicVO> itripLabelDicVOS = new ArrayList<ItripLabelDicVO>(0);
        Map map = new HashMap();
        if (parentId != null) {
            map.put("parentId", parentId);
            labelDics = itripLabelDicMapper.selectAllMessageByParId(map);
        }else {
            dto.setCode("10086");
            dto.setMsg("parentId不能为空");
            dto.setSuccess("false");
            return dto;
        }
        if (labelDics.size() > 0 && !labelDics.isEmpty()) {
            for (ItripLabelDic itripLabelDic : labelDics) {
                ItripLabelDicVO itripLabelDicVO = new ItripLabelDicVO();
                itripLabelDicVO.setId(itripLabelDic.getId());
                itripLabelDicVO.setName(itripLabelDic.getName());
                itripLabelDicVO.setDescription(itripLabelDic.getDescription());
                itripLabelDicVO.setPic(itripLabelDic.getPic());
                itripLabelDicVOS.add(itripLabelDicVO);
            }
            dto.setCode("0");
            dto.setMsg("查询成功");
            dto.setSuccess("true");
            dto.setData(itripLabelDicVOS);
            return dto;
        } else {
            dto.setCode("10086");
            dto.setMsg("查询失败");
            dto.setSuccess("false");
            return dto;
        }

    }

    @Override
    public ItripHotelRoom senectById(Long id) {
        ItripHotelRoom itripHotelRoom=new ItripHotelRoom();
        if(id!=null){
            itripHotelRoom=itripHotelRoomMapper.selectByPrimaryKey(id);
        }
        return itripHotelRoom;
    }




}
